<?php

namespace app\api\controller\v41;

use app\api\controller\v2\Auth;
use app\common\cachemodel\active\Active1CouponRecord;
use app\common\cachemodel\UserCoupon;
use app\common\cachemodel\ZlCouponAttr;
use app\common\lib\SensorsData;
use app\common\model\Goods;

/**
 * 2024年启示录 领取优惠券接口
 */
class YearActive2023 extends Auth
{
    public $activeFlag = 20240101;  // 2024年启示录 活动标识

    private $couponData = [
        732 => 0.57,   // 年运八折券    优惠券ID => 获得机率
        733 => 0.15,   // 年运7折券
        731 => 0.25,   // 年运九折券
        734 => 0.02,   // 年运5折券
        730 => 0.01,   // 年运免费券
    ];

    private $couponName = [
        732 => '年运八折券',   // 年运八折券    优惠券ID => 获得机率
        733 => '年运七折券',   // 年运7折券
        731 => '年运九折券',   // 年运九折券
        734 => '年运五折券',   // 年运5折券
        730 => '年运免费券',   // 年运免费券
    ];

    /**
     * 根据概率获取优惠券ID
     * @param array $couponData
     * @return int
     * @throws \Exception
     */
    protected function randomGetCid(array $couponData): int
    {
        $num = random_int(1, 10000000);
        $resScale = 0;
        foreach ($couponData as $cid => $percent)
        {
            $resScale = $resScale + $percent * 10000000;  // 此次因为要除以100  所以直接*的时候少两个0

            if ($num <= $resScale)
            {   // 中奖了
                return $cid;
            }
        }

        return 0;
    }

    public function getCoupon()
    {
        $activeCouponRecordModel = new Active1CouponRecord();
        $giveCouponRecord = $activeCouponRecordModel->getCouponData($this->activeFlag, $this->user['id'], 3);
        if (isset($giveCouponRecord[1]))
        {
            return show(config('code.error'), '优惠券已经领取过了');
        }
        // 发放优惠券
        $cid = $this->randomGetCid($this->couponData);
        // 发放优惠券
        $userCouponModel = new UserCoupon();
        $couponInfo = (new ZlCouponAttr())->getInfoById($cid, ['desc', 'type', 'rand_min', 'rand_max', 'ur_id', 'gids', 'discount']);
        $cData = [
            'uid' => $this->user['id'],
            'c_id' => $cid,
            'ur_id' => $couponInfo['ur_id'],
            'start_time' => $this->request->time(),
            'end_time' => strtotime("+30 day"),
            'give_time' => $this->request->time(),
            'status' => 0,
            'price' => $couponInfo['type'] == 4 ? mt_rand($couponInfo['rand_min'], $couponInfo['rand_max']) : 0,
            'is_give' => 0,
            'create_time' => $this->request->time(),
            'update_time' => 0,
        ];
        $ucId = $userCouponModel->insertGetId($cData);
        $ids[$ucId] = $ucId; // 添加未用于ID
        // 神策打点
        $gidsArr = explode(',', $couponInfo['gids']);
        $couponRangeArr = (new Goods())->where('id', 'in', $gidsArr)->column('name');
        // 神策打点 75 获得优惠券 AcquireCoupon
        SensorsData::track($this->user['id'], true, 'AcquireCoupon', [
            'refer_name' => '',
            'refer_link' => '',
            'coupon_name' => $couponInfo['desc'],
            'coupon_expire' => date('Y-m-d H:i:s', $cData['end_time']),
            'coupon_type' => $userCouponModel->couponTypeName[$couponInfo['type']],
            'coupon_range' => $couponRangeArr,
            'acquire_channel' => '2024年启示录 - 领取优惠券',
            'is_give' => false,
            'acquire_scene' => "",
        ]);
        // 写入 活动 获得优惠券记录
        (new Active1CouponRecord())->SaveData([
            'active_flag' => $this->activeFlag,
            'uid' => $this->user['id'],
            'type' => 3,
            'flag' => 1,   //
            'cid' => $ucId,
            'create_time' => $this->request->time(),
        ]);
        // 未使用
        $userCouponModel->listDataAdd($this->user['id'], $ids, 1);

        return show(config('code.success'), '领取成功', [
            'coupon_name' => $this->couponName[$cid],
        ]);
    }
}